<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 优惠券
 */
class Coupon extends Api
{
    protected $noNeedLogin = ['index'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Coupon;

    }
    /**
     * 列表
     *
     */
    public function index()
    {
        $page = (int)$this->request->request('page');
        $page = empty($page) ? 0 : ($page - 1) * 10;
        
        $coupon_ids = db('coupon_history')->where('user_id',$this->auth->id)->column('coupon_id');

        $data = $this->model->field('id,name,type,goods_type,discount')->where('type',0)->where('status',0)->limit($page,20)->select();

        foreach($data as $v){
            if(in_array($v['id'],$coupon_ids)){
                $v['is_select'] = 1;
            }else{
                $v['is_select'] = 0;

            }
        }
        $this->success('请求成功',$data);
    }

    
    
}
